package com.imonsoft.sqm.service.sys;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.TreeGridNode;
import com.imonsoft.sqm.dao.sys.FunctionDAO;
import com.imonsoft.sqm.pojo.sys.Function;
import com.imonsoft.sqm.pojo.sys.FunctionExample;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("functionService")
public class FunctionServiceImpl implements FunctionService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private FunctionDAO functionDAO;
	@Autowired
	private RoleService roleService;

	@Override
	public void deleteFunction(List<Long> idList) {
		if (null == idList || idList.isEmpty()) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		for (int i = 0; i < idList.size(); i++) {
			// 删除角色与资源的关系表
			roleService.deleteRoleFunctionByFunction(idList.get(i));
			delete(idList.get(i));
		}
	}

	@Override
	public List<Function> findFunctionByCode(String code) {
		if (StringUtils.isEmpty(code)) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		FunctionExample example = new FunctionExample();
		FunctionExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(code)) {
			criteria.andCodeEqualTo(code);
		}
		List<Function> functionList = functionDAO.selectFunctionByExample(example);
		return functionList;
	}

	@Override
	public Long insert(Function entity) {
		if (null == entity) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		entity.setState(ConstantSqm.STATUS_NORMAL);
		return functionDAO.insertFunction(entity);
	}

	@Override
	public int update(Function entity) {
		if (null == entity || null == entity.getId()) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		return functionDAO.updateFunctionByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(Long id) {
		if (null == id) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		return functionDAO.deleteFunctionByPrimaryKey(id);
	}

	@Override
	public Function get(Long id) {
		if (null == id) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		Function function = functionDAO.selectFunctionByPrimaryKey(id);
		return function;
	}

	@Override
	public List<Function> findList(Function entity) {
		FunctionExample example = new FunctionExample();
		example.setOrderByClause("row_no asc");
		FunctionExample.Criteria criteria = example.createCriteria();
		if (null != entity) {
			if (!StringUtils.isEmpty(entity.getCode())) {
				criteria.andCodeLike(entity.getCode() + "%");
			}
		}
		List<Function> functionList = functionDAO.selectFunctionByExample(example);
		return functionList;
	}

	@Override
	public DataGrid<Function> getDataGrid(Function entity, Pager pager) {
		FunctionExample example = new FunctionExample();
		example.setOrderByClause("id desc");
		FunctionExample.Criteria criteria = example.createCriteria();
		if (null != entity) {
			if (!StringUtils.isEmpty(entity.getCode())) {
				criteria.andCodeLike(entity.getCode() + "%");
			}
			if (!StringUtils.isEmpty(entity.getState())) {
				criteria.andStateEqualTo(entity.getState());
			}
		}
		return functionDAO.getDataGrid(example, pager);
	}

	@Override
	public List<TreeGridNode> getAlltreeGridNodes() {
		List<TreeGridNode> reslutList = new ArrayList<TreeGridNode>();
		FunctionExample example = new FunctionExample();
		example.setOrderByClause("row_no asc");
		List<Function> list = functionDAO.selectFunctionByExample(example);
		for (Function function : list) {
			TreeGridNode treeGridNode = function.createTreeGridNode();
			reslutList.add(treeGridNode);
		}
		return reslutList;
	}
}
